<!--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  -->

<%@tag import="org.virtualbox_3_2.HostNetworkInterfaceType"%>
<%@tag import="Utilities.Constants.NetworkType"%>
<%@tag import="Beans.Responses.DataRetrievalComponents.NetworkDataComponent"%>
<%@tag import="Beans.Responses.DataRetrievalResponse"%>
<%@tag import="Utilities.Constants.ResourceRequestType"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc" %>
<%@tag description="Prints network(s) data." pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>

<%-- Attributes --%>
<%@attribute name="data" required="true" description="Network(s) data response object."
             rtexprvalue="true" type="DataRetrievalResponse<NetworkDataComponent>"%>

<%@attribute name="type" required="true" description="Network type (1->Internal; 2->Host-Only)."
             rtexprvalue="true" type="java.lang.Integer"%>

<%@attribute name="targetFieldName" required="false" 
             description="The name of a form's filed, to be used when the page is loaded for network selection."
             rtexprvalue="true" type="java.lang.String"%>
<%-- End of Attributes --%>

<%-- Page Body Content --%>
<jstlc:choose>
    <jstlc:when test="${data == null}">
        Failed to retrieve network(s) data object.
    </jstlc:when>

    <jstlc:when test="${data.requestResult && data.valid}">
        <jstlc:choose>
            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.SINGLE_RESOURCE %>">
                <table id="mainNetworkTable" class="mainResourceDataTable">
                    <tr><td>Network ID:</td><td>${data.resource.networkData.resourceId}</td></tr>
                    <tr><td>Network Name:</td><td>${data.resource.networkName}</td></tr>
                    <tr><td>Server ID:</td><td>${data.resource.networkData.serverId}</td></tr>
                    <tr><td>Owner ID:</td><td>${data.resource.networkData.ownerId}</td></tr>
                    <tr><td>Creation Date:</td><td>${data.resource.networkData.dateCreated}</td></tr>
                    <tr><td>Permissions:</td><td>${data.resource.networkData.permissions}</td></tr>
                    <tr><td>Network Type:</td><td>${data.resource.networkType}</td></tr>
                    <tr><td>Details:</td>
                        <td><a class="actionLink" href="noJavaScript.jsp"
                               onclick="return changePopupVisibility('additionalNetworkInfoTable');">View</a></td></tr>
                </table>
                <div id="networkOperations">
                    <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formModifyNetwork');">Modify Network</a><br>
                    <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('dhcpServerTable');">DHCP Server</a><br>
                </div>
                <jstlc:choose>
                    <jstlc:when test="<%= data.getResource().getNetworkType() == NetworkType.INTERNAL %>">
                        <jstlc:set var="network" value="${data.resource.internalNetwork}"/>
                        <table id="additionalNetworkInfoTable" class="resourceDataTable">
                            <tr><td>Attached NICs:</td><td>${network.internalNetworkData.numberOfAttachedNICs}</td></tr>
                        </table>
                        <form class="resourceModification" id="formModifyNetwork" action="manage_network.jsp?action=6&amp;id=${data.resource.networkData.resourceId}" method="post">
                            Modify Internal Network:<br>
                            <table>
                            <tr><td>External Name:</td><td><input type="text" name="modify.internal.name" value="${network.internalNetworkData.networkName}"></td>
                            <td><input type="submit" name="modify.internal.name.submit" value="Update"></td></tr>
                            <tr><td>Permissions:</td><td><input type="text" size="2" name="modify.internal.permissions" value="${data.resource.networkData.permissions}"></td>
                            <td><input type="submit" name="modify.internal.permissions.submit" value="Update"></td></tr>
                            </table>
                        </form>
                    </jstlc:when>

                    <jstlc:when test="<%= data.getResource().getNetworkType() == NetworkType.HOST_ONLY %>">
                        <jstlc:set var="network" value="${data.resource.hostNetwork}"/>
                        <table id="additionalNetworkInfoTable" class="resourceDataTable">
                            <tr><td>Interface Name:</td><td>${network.interfaceName}</td></tr>
                            <tr><td>IPv4 Address:</td><td>${network.IPv4Address}</td></tr>
                            <tr><td>IPv4 Mask:</td><td>${network.IPv4Mask}</td></tr>
                            <tr><td>IPv6 Address:</td><td>${network.IPv6Address}</td></tr>
                            <tr><td>IPv6 Mask Length:</td><td>${network.IPv6MaskPrefixLength}</td></tr>
                            <tr><td>IPv6 Supported:</td><td>${network.IPv6Supported ? "Yes" : "No"}</td></tr>
                            <tr><td>Physical Address:</td><td>${network.hardwareAddress}</td></tr>
                            <tr><td>Encapsulation:</td><td>${network.protocolEncapsulation}</td></tr>
                            <tr><td>Interface State:</td><td>${network.interfaceStatus}</td></tr>
                            <tr><td>Interface Type:</td><td>${network.interfaceType}</td></tr>
                        </table>
                        <form class="resourceModification" id="formModifyNetwork" action="manage_network.jsp?action=5&amp;id=${data.resource.networkData.resourceId}" method="post">
                            Modify Host Interface:<br>
                            <table>
                            <tr><td>IP Configuration:</td>
                            <td><select name="modify.hostif.ipConfig" onchange="updateNetworkConfigurationFields('formModifyNetwork', 'modify.hostif');">
                                <option value="0" selected disabled>Select</option>
                                <option value="dynamic" disabled>Dynamic</option>
                                <option value="staticV4">Static (IPv4)</option>
                                <option value="staticV6">Static (IPv6)</option>
                            </select></td></tr>
                            <tr><td>IP Address:</td><td><input type="text" size="12" name="modify.hostif.address" disabled></td></tr>
                            <tr><td>Network Mask:</td><td><input type="text" size="12" name="modify.hostif.ipv4mask" disabled></td></tr>
                            <tr><td>Mask Length:</td><td><input type="text" size="2" name="modify.hostif.ipv6mask" disabled></td></tr>
                            <tr><td>-</td><td>-</td><td><input type="submit" value="Change"></td></tr>
                            <tr><td>Permissions:</td><td><input type="text" size="2" name="modify.hostif.permissions" value="${data.resource.networkData.permissions}"></td>
                            <td><input type="submit" name="modify.hostif.permissions.submit" value="Update"></td></tr>
                            <tr><td>Owner ID:</td><td><input type="text" size="3" name="modify.hostif.newOwner" value="${data.resource.networkData.ownerId}"></td>
                            <td><input type="submit" name="modify.hostif.newOwner.submit" value="Update"></td></tr>
                            </table>
                        </form>
                    </jstlc:when>

                    <jstlc:otherwise>
                        Invalid network type.
                    </jstlc:otherwise>
                </jstlc:choose>
                <table id="dhcpServerTable" class="resourceModification">
                <jstlc:choose>
                    <jstlc:when test="${data.resource.DHCPAvailable}">
                        <form class="resourceModification" id="formModifyDHCP" action="manage_network.jsp?type=${type}&amp;action=4&amp;id=${data.resource.networkData.resourceId}" method="post">
                        <tr><td>DHCP Server:</td><td>Available</td></tr>
                        <tr><td>State:</td><td>${network.dhcpServer.serverState ? "Enabled" : "Disabled"} /
                                <input type="submit" name="modify.dhcp.${network.dhcpServer.serverState ? "disable" : "enable"}.submit"
                                                     value="${network.dhcpServer.serverState ? "Disable" : "Enable"}"></td></tr>
                        <tr><td>IP Address:</td><td><input type="text" size="12" name="modify.dhcp.ipAddress" value="${network.dhcpServer.serverIPAddress}"></td></tr>
                        <tr><td>Mask:</td><td><input type="text" size="12" name="modify.dhcp.networkMask" value="${network.dhcpServer.serverNetworkMask}"></td></tr>
                        <tr><td>Address Range:</td><td><input type="text" size="12" name="modify.dhcp.addressLow" value="${network.dhcpServer.addressRangeLow}">
                                to <input type="text" size="12" name="modify.dhcp.addressHigh" value="${network.dhcpServer.addressRangeHigh}"></td></tr>
                        <tr><td><input type="submit" name="modify.dhcp.config.submit" value="Update"></td>
                            <td><a class="actionLink" href="manage_network.jsp?type=${type}&amp;action=7&amp;id=${data.resource.networkData.resourceId}">Remove</a></td></tr>
                        </form>
                    </jstlc:when>
                    <jstlc:when test="<%= data.getResource().getInterfaceType() != HostNetworkInterfaceType.BRIDGED %>">
                        <tr><td>DHCP Server:</td><td>Not Available</td></tr>
                        <tr><td><a class="actionLink" href="noJavaSctipt.jsp" onclick="return changePopupVisibility('formCreateDHCP', true);">Create DHCP</a></td></tr>
                    </jstlc:when>
                    <jstlc:otherwise>
                        <tr><td>DHCP Server creation is not available for bridged networks.</td></tr>
                    </jstlc:otherwise>
                </jstlc:choose>
                </table>
                <form class="resourceCreationElement" id="formCreateDHCP" action="manage_network.jsp?type=${type}&amp;action=1&amp;id=${data.resource.networkData.resourceId}" method="post">
                    Create DHCP:<br>
                    <table>
                    <tr><td>Enabled:</td><td><input type="checkbox" name="create.dhcp.enable"></td></tr>
                    <tr><td>IP Address:</td><td><input type="text" size="12" name="create.dhcp.ipAddress"></td></tr>
                    <tr><td>Network Mask:</td><td><input type="text" size="12" name="create.dhcp.networkMask"></td></tr>
                    <tr><td>Address Range:</td><td><input type="text" size="12" name="create.dhcp.addressLow">
                            to <input type="text" size="12" name="create.dhcp.addressHigh"></td></tr>
                    <tr><td><input type="submit" value="Create"></td></tr>
                    </table>
                </form>
            </jstlc:when>

            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.MULTIPLE_RESOURCES %>">
                <div id="networksSelectionElement">
                <table id="networksTable" class="resourcesTable">
                    <tr><td>UUID</td><td>Name</td><td>Server</td><td>Owner</td><td></td><td></td>
                    <jstlc:choose>
                        <jstlc:when test="${targetFieldName != null}">
                            <jstlc:forEach var="network" items="${data.resourcesList}">
                                <tr><td>${network.networkData.resourceId}</td><td>${network.networkName}</td><td>${network.networkData.serverId}</td><td>${network.networkData.ownerId}</td>
                                    <td><a class="operationsLink" href="noJavaScript.jsp"
                                           onclick="return makeNetworkResourceSelection('${targetFieldName}', '${network.networkData.resourceId}', '${network.networkName}');">Select</a></td>
                                    <td></td></tr>
                            </jstlc:forEach>
                        </jstlc:when>
                        <jstlc:otherwise>
                            <jstlc:forEach var="network" items="${data.resourcesList}">
                                <tr><td>${network.networkData.resourceId}</td><td>${network.networkName}</td>
                                    <td><a href="servers.jsp?id=${network.networkData.serverId}">${network.networkData.serverId}</a></td>
                                    <td><a href="users.jsp?id=${network.networkData.ownerId}">${network.networkData.ownerId}</a></td>
                                    <td><a class="operationsLink" href="networks.jsp?type=${type}&amp;id=${network.networkData.resourceId}">View</a></td>
                                    <td><a class="operationsLink" href="manage_network.jsp?action=${10 - type}&amp;id=${network.networkData.resourceId}"
                                           onclick="return resourceRemovalVerification('${network.networkName}', this);">Remove</a></td></tr>
                            </jstlc:forEach>
                        </jstlc:otherwise>
                    </jstlc:choose>
                </table>
                <script type="text/javascript">
                    var networksPagination = new ElementsTablePagination('networksTable', 'networksPagination');
                </script>
                Page Size: <input type="text" size="2" id="itemsPerPage" value="20">
                <a class="pageSizeUpdateLink" href="noJavaScript.jsp"
                   onclick="return networksPagination.setMaxNumberOfItemsPerPage(document.getElementById('itemsPerPage').value);">Update</a>
                <jstlc:if test="${targetFieldName != null}">
                    <a class="actionLink" href="noJavaScript.jsp" onclick="self.close();">Close</a>
                    <script type="text/javascript">
                        selectionWindowModification('networksSelectionElement');
                    </script>
                </jstlc:if>
                </div>
                <div class="creationOperations">
                <jstlc:choose>
                    <jstlc:when test="${type == 1}">
                    <a class="creationLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formCreateInternalNetwork');">Create Internal Network</a>
                    <form class="resourceCreationElement" id="formCreateInternalNetwork" action="manage_network.jsp?action=3" method="post">
                        <table>
                            <tr><td>Create Internal Network:</td></tr>
                            <tr><td>Network Name:</td><td><input type="text" name="create.internal.name"></td></tr>
                            <tr><td>Permissions:</td><td><input type="text" size="2" name="create.internal.permissions"></td></tr>
                            <tr><td><input type="submit" value="Create"></td></tr>
                        </table>
                    </form>
                    </jstlc:when>
                    <jstlc:when test="${type == 2}">
                    <a class="creationLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formCreateHostOnlyIf');">Create Host Interface</a>
                    <form class="resourceCreationElement" id="formCreateHostOnlyIf" action="manage_network.jsp?action=2" method="post">
                        <table>
                            <tr><td>Create Host-Only Interface:</td></tr>
                            <tr><td>Permissions:</td><td><input type="text" size="2" name="create.hostOnly.permissions"></td></tr>
                            <tr><td><input type="submit" value="Create"></td></tr>
                        </table>
                    </form>
                    </jstlc:when>
                </jstlc:choose>
                </div>
            </jstlc:when>

            <jstlc:otherwise>
                Invalid resource request type: ${data.resourceRequestType}.
            </jstlc:otherwise>
        </jstlc:choose>
    </jstlc:when>

    <jstlc:otherwise>
        Error: ${data.exception.exceptionMessage}
    </jstlc:otherwise>
</jstlc:choose>
<%-- End of Page Body Content --%>